﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using NHibernate;
using CsharpTrainer.NHibernate3;
using CsharpTrainer.NHibernate3.Mapping;
using CsharpTrainer.NHibernate3.Entities;
using Zeus.Thunder.Core;

namespace SharpTrainer.NHibernate
{
    class HbQueryTest1 : ITestCase
    {
        public void Run()
        {
            string connStr = ConfigurationManager.ConnectionStrings["NorthwindConnStr"].ConnectionString;

            try
            {
                NHibernateDb<EmployeeMap> db = new NHibernateDb<EmployeeMap>(connStr);

                ISessionFactory factory = db.SessionFactory;
                using (var session = factory.OpenSession())
                {
                    var employees = session.QueryOver<Employees>()
                                    .OrderBy(a => a.LastName).Asc
                                    .List();
                    Console.WriteLine("** All Employees Below **");
                    Display(employees);

                    employees =  session.QueryOver<Employees>()
                                 .OrderBy(a => a.LastName).Asc.Take(3)
                                 .List();
                    Console.WriteLine("** Only Take the First 3 Employees **");
                    Display(employees);

                    employees = session.CreateQuery("from Employees where Title='Sales Manager'").List<Employees>();
                    Console.WriteLine("** Select the Sales Manager **");
                    Display(employees);
                }
                
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception: " + ex.ToString());
            }
        }

        private void Display(IEnumerable<Employees> employees)
        {
            foreach (var employee in employees)
            {
                Console.WriteLine(employee);
            }
        }
    }
}
